<template>
  <div>
    <!-- 题型基本信息 -->
    <el-row :gutter="20" style="margin-bottom: 20px">
      <el-col :span="6">【题型】：{{ detailData.questionType | formatterQuestionType }}题</el-col>
      <el-col :span="6">【编号】：{{ detailData.id }}</el-col>
      <el-col :span="6">【难度】：{{ detailData.difficulty | formatterDifficulty }}</el-col>
      <el-col :span="6">【标签】：{{ detailData.tags }}</el-col>
    </el-row>
    <el-row :gutter="20" style="margin-bottom: 20px">
      <el-col :span="6">【学科】：{{ detailData.subjectName }}</el-col>
      <el-col :span="6">【目录】：{{ detailData.directoryName }}</el-col>
      <el-col :span="6">【方向】：{{ detailData.direction }}</el-col>
      <el-col :span="6"></el-col>
    </el-row>

    <!-- 分割线 -->
    <el-divider></el-divider>

    <!-- 题干 -->
    <div>【题干】:</div>
    <div v-html="detailData.question" style="color: blue"></div>
    <p v-if="detailData.questionType === '3'">{{ detailData.questionType | formatterQuestionType }}题 选项：（以下选中的选项为正确答案</p>
    <!-- 单选 -->
    <template v-if="detailData.questionType === '1'">
      <div class="readonlyRadio" v-for="item in detailData.options" :key="item.id">
        <el-radio :value="radio" :label="item.code">{{ item.title }}</el-radio>
      </div>
    </template>

    <!-- 多选 -->
    <template v-else-if="detailData.questionType === '2'" >
      <div class="readonlyRadio" v-for="item in detailData.options" :key="item.id">
        <el-checkbox  :value="checkedList" :label="item.code">{{ item.title }}</el-checkbox>
      </div>
    </template>

    <!-- 分割线 -->
    <el-divider></el-divider>

    <!-- 参考答案 -->
    <div>【参考答案】：
      <el-button type="danger" @click="isShow=true">视频答案预览</el-button>
      <div class="video" v-show="isShow" >
        <video controls="controls" src="detailData.videoURL"></video>
      </div>
    </div>

    <!-- 分割线 -->
    <el-divider></el-divider>

    <!-- 答案解析 -->
    【答案解析】：<div style="display: inline-block;" v-html="detailData.answer"></div>

    <!-- 参考答案 -->
    <el-divider></el-divider>

    <!-- 题目备注 -->
    <div>【题目备注】：{{ detailData.remarks }}</div>
  </div>
</template>

<script>
import { detail } from '@/api/hmmm/questions'
import { questionType, difficulty } from '@/api/hmmm/constants'

export default {
  name: 'QuestionsDialog',
  props: {
    id: {
      type: Number,
      required: true
    }
  },
  filters: {
    formatterQuestionType (val) {
      const obj = questionType.find(item => item.value === +val)
      return obj ? obj.label : '未知'
    },
    formatterDifficulty (val) {
      const obj = difficulty.find(item => item.value === +val)
      return obj ? obj.label : '未知'
    }
  },
  data () {
    return {
      detailData: {},
      isShow: false,
      // 多选
      checkedList: [],
      // 单选
      radio: ''
    }
  },
  watch: {
    detailData: function (newVal) {
      if (newVal.questionType === '1') {
        // 单选
        this.radio = newVal.options.filter(item => item.isRight === 1).map(item => item.code)[0]
        return this.radio
      }
      if (newVal.questionType === '2') {
        this.checkedList = newVal.options.filter(item => item.isRight === 1).map(item => item.code)
        return this.checkedList
      }
    },
    deep: true,
    immediate: true
  },
  created () {
    // 获取当前预览详情
    this.getQuestionsData()
  },
  methods: {
    // 获取当前预览详情
    async getQuestionsData () {
      const res = await detail({ id: this.id })
      this.detailData = res.data
      console.log(res)
    }
  }
}
</script>

<style scoped>
.readonlyRadio {
  display: flex;
  flex-direction: column;
}
.readonlyRadio label {
  margin-bottom: 8px;
}
.video {
  width: 400px;
  height: 300px;
}
.video video {
  width: 100%;
  height: 100%;
}
</style>
